package com.xnfh.home.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.gyf.immersionbar.ktx.immersionBar
import com.xnfh.common.constant.ArouterPath
import com.xnfh.common.constant.IntentKey.KEY_INDEX
import com.xnfh.framework.base.BaseActivity
import com.xnfh.framework.helper.SumAppHelper
import com.xnfh.framework.log.LogUtil
import com.xnfh.framework.toast.TipsToast
import com.xnfh.framework.utils.AppExit
import com.xnfh.home.R
import com.xnfh.home.bean.TabEntity
import com.xnfh.home.databinding.ActivityMainLayoutBinding
import com.xnfh.home.ui.fragment.HomeFragment

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/11/8 11:38
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/11/8 11:38
 * @UpdateRemark: 更新说明
 */
@Route(path = ArouterPath.HOME.MAIN_ACTIVITY)
class MainActivity : BaseActivity<ActivityMainLayoutBinding>() {
    override fun getLayoutID(): Int = R.layout.activity_main_layout
    private val mTitles = arrayOf(
        SumAppHelper.getApplication().resources.getString(R.string.tab_home),
        SumAppHelper.getApplication().resources.getString(R.string.tab_field),
        SumAppHelper.getApplication().resources.getString(R.string.tab_zzservice),
        SumAppHelper.getApplication().resources.getString(R.string.tab_sbgl),
        SumAppHelper.getApplication().resources.getString(R.string.tab_mine),
    )
    private val mIconSelectIds = intArrayOf(
        R.mipmap.ic_navi_home_select,
        R.mipmap.ic_navi_field_select,
        R.mipmap.ic_navi_zzservice_select,
        R.mipmap.ic_navi_zzservice_select,
        R.mipmap.ic_navi_mine_select
    )
    private val mIconUnselectIds = intArrayOf(
        R.mipmap.ic_navi_home,
        R.mipmap.ic_navi_field,
        R.mipmap.ic_navi_zzservice,
        R.mipmap.ic_navi_zzservice,
        R.mipmap.ic_navi_mine
    )

    private var currentFragment: Fragment? = null

    private var fragments = ArrayList<Fragment>()

    companion object {
        fun start(context: Context, index: Int = 0) {
            val intent = Intent(context, MainActivity::class.java)
            intent.putExtra(KEY_INDEX, index)
            context.startActivity(intent)
        }
    }

    override fun init(savedInstanceState: Bundle?) {
        val mTabEntities = ArrayList<CustomTabEntity>()
        for (i in mTitles.indices) {
            mTabEntities.add(
                TabEntity(
                    mTitles[i],
                    mIconSelectIds[i],
                    mIconUnselectIds[i]
                )
            )
        }
        fragments.add(HomeFragment())
        val fieldFragment = ARouter.getInstance().build(ArouterPath.FIELD.FIELD_FRAGMENT).navigation() as Fragment
        fragments.add(fieldFragment)
        val plantingFragment = ARouter.getInstance().build(ArouterPath.PLANTING.PLANTING_FRAGMENT).navigation() as Fragment
        fragments.add(plantingFragment)
        val equipmentFragment = ARouter.getInstance().build(ArouterPath.EQUIPMENT.EQUIPMENT_FRAGMENT).navigation() as Fragment
        fragments.add(equipmentFragment)
        val mineFragment = ARouter.getInstance().build(ArouterPath.MINE.MINE_FRAGMENT).navigation() as Fragment
        fragments.add(mineFragment)
        //默认展示第一个fragment
        switchFragment(fragments[0]).commit()
        mBind.homeTablayout.setTabData(mTabEntities)
        mBind.homeTablayout.currentTab = 0
        mBind.homeTablayout.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                val fragment = fragments[position]
                switchFragment(fragment).commit()
            }

            override fun onTabReselect(position: Int) {
            }
        })

    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        intent?.let {
            val index = intent.getIntExtra(KEY_INDEX, 0)
            val fragment = fragments[index]
            switchFragment(fragment).commit()
            mBind.homeTablayout.currentTab = index
        }
    }

    override fun observe() {
    }


    private fun switchFragment(fragment: Fragment): FragmentTransaction {
        val transaction = supportFragmentManager.beginTransaction()
        if (fragment !== currentFragment) {   //如果相等不用做任何操作 所以加了此判断
            if (!fragment.isAdded) {
                if (currentFragment != null) {
                    currentFragment?.let {
                        transaction.hide(it)
                    }
                }
                transaction.add(R.id.home_frame, fragment) //如果没有添加过则添加
            } else {
                currentFragment?.let {
                    transaction.hide(it).show(fragment) //如果添加过则直接隐藏之前的Fragment并显示出来
                }
            }
            currentFragment = fragment
        }
        return transaction
    }

    override fun onBackPressed() {
        AppExit.onBackPressed(
            this,
            { TipsToast.showTips(getString(R.string.app_exit_one_more_press)) }
        )
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        fragments.forEach {
            it.onActivityResult(requestCode, resultCode, data)
        }
    }
    override fun initImmersionBar() {
        immersionBar {
            statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
        }
    }
}